<?php
            //以UTF-8编码显示
            function utf8() {
                header("Content-type:text/html;charset=utf-8");
            }

            //获取仓库的信息
            function getCommit($url = 'https://gitee.com/api/v5/repos/w169cn/123/commits?page=1&per_page=200') {
                // 初始化curl工具
                $curl = curl_init($url);

                // 进行必要的配置
                curl_setopt($curl, CURLOPT_HEADER, 0);
                // 取消curl显示返回内容
                curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
                // 跳过证书检查
                curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
                //从证书中检查SSL加密算法是否存在
                curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
                // 执行curl
                $data = curl_exec($curl);
                // 对data进行解码，将其从json格式转化为php数组
                $data = json_decode($data, true);
                curl_close($curl);
                return $data;
            }

            //获得提交时间信息
            function getCommitByWeek(array $data) {
                // 提取每一次作业的提交时间
                $commitDate = array();
                foreach ($data as $key => $value) {
                    $commitDate[$key] = strtotime($data[$key]['commit']['commiter']['date']);
                }
                return $commitDate;
            }

            //周次节点信息
            function weekly($start = '2018-03-05') {
                for ($i = 0; $i <=19; $i++) {
                    $data[$i] = strtotime("+" . $i . " week", strtotime($start));
                }
                return $data;
            }

            //获取每周作业推送次数
            function getCountByWeek(array $commitDate = array()) {
                // 先得到学期时间节点
                $week = weekly();
                // 初始化数组
                $array = array();
                // 逐一比较
                for ($i = 0; $i <=19; $i++) {
                    $array[$i] = 0;
                    foreach ($commitDate as $key => $value) {
                        if ($value >= $week[$i] && $value <= $week[$i + 1]) {
                            $array[$i]++;
                        }
                    }
                }
                return $array;
            }

            //生成作业报告
            function report(array $data, array $countByWeek) {
                $chengji = 0;
                // echo '我的作业地址是：' . 'https://gitee.com/api/v5/repos/w169cn/123/commits?page=1&per_page=200' . '<br>';
                // echo '提交次数：' . count($data) . '次。<br>';
                foreach ($countByWeek as $key => $value) {
                    // echo '第' . ($key + 1) . '周提交作业' . $value . '次。<br>';
                    // 判断是否提交和总成绩是否超过100
                    if($value > 0 && $chengji < 100){
                        $chengji = $chengji + 10;
                    }
                }
                // echo '我的总成绩为：' . $chengji . '<br>';
                return $chengji;
            }

            //运行部分
            utf8();
            $url = 'https://gitee.com/api/v5/repos/w169cn/123/commits?page=1&per_page=200';
            $data = getCommit($url);
            $commitDate  = getCommitByWeek($data);
            $countByWeek = getCountByWeek($commitDate);
            $score = report($data, $countByWeek);
            ?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" href="3.3.7.css">
</head>
<body>
    <div class="container">
    <h1 class="text-center" align="center">作业提交信息</h1>
    <table class="table table-bordered table-striped" align="center" style="border:solid 1px #000000" 
        <tr>
            <td width="30%" class="danger" style="background-color: #F0FFF0" >作业地址</td><td style="background-color: #F4F4F4">https://gitee.com/w169cn/123/php_weige.git</td>
        </tr>
        <tr>
            <td class="danger" style="background-color: #F0FFF0">提交次数</td><td  align="center"style="background-color: #F4F4F4"><?php echo count($data); ?></td>
        </tr>
        <?php foreach ($countByWeek as $key => $value): ?>
         <tr>
            <td class="danger" style="background-color: #F0FFF0"><?php echo '第'. ($key + 1) . '周提交次数' ?></td>
            <td style="background-color: #F4F4F4" align="center"><?php echo $value ?></td>
        </tr>
        <?php endforeach?>
        <tr class="info">
            <td style="background-color: #F0FFF0">平时成绩</td><td  align="center" style="background-color: #F4F4F4"><?php echo $score ?></td>
        </tr>
    </table>
    </div>
</body>
</html>